from django.db import models
import datetime


# Create your models here.

# 客户表
class Customer(models.Model):
    name = models.CharField(max_length=200) # 姓名
    phonenumber = models.CharField(max_length=200) # 电话
    address = models.CharField(max_length=200) # 地址

# 药品表
class Medicine(models.Model):   
    name = models.CharField(max_length=200) # 药品名
    sn = models.CharField(max_length=200) # 药品编号
    desc = models.CharField(max_length=200) # 描述

# 订单表
class Order(models.Model):
    name = models.CharField(max_length=200, null=True, blank=True) # 订单名
    create_date = models.DateTimeField(default=datetime.datetime.now) # 创建日期
    customer = models.ForeignKey(Customer, on_delete=models.PROTECT) # 外键客户，第一个参数指向Customer表的主键
    medicine = models.ManyToManyField(Medicine, through='OrderMedicine') # 订单购买的药品，和Medicine表是多对多关系
    medicinelist = models.CharField(max_length=200, blank=True, null=True)

# 多对多关系表
class OrderMedicine(models.Model):
    order = models.ForeignKey(Order, on_delete=models.PROTECT)
    medicine = models.ForeignKey(Medicine, on_delete=models.PROTECT)
    amount = models.PositiveIntegerField() # 订单中药品的数量
